package com.zretc.device.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.util.List;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import com.zretc.device.dao.DeviceDao;
import com.zretc.device.dao.Device_AdminDao;
import com.zretc.device.dao.Device_OutDao;
import com.zretc.device.entity.Device;
import com.zretc.device.entity.Device_Admin;

import javax.swing.DefaultComboBoxModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPopupMenu;
import java.awt.Component;
import javax.swing.JComboBox;

public class MenuFrameDevAdmin extends JFrame {

	private JPanel contentPane;
	private JTextField txtInput;
	private JTable table;
	private JTable table_1;
	private Device device1;
	private String devCode;
	private JTextField txtName;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MenuFrameDevAdmin frame = new MenuFrameDevAdmin();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MenuFrameDevAdmin() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 822, 618);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnNewMenu = new JMenu("库存管理");
		menuBar.add(mnNewMenu);
		
		//滚动条
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(63, 182, 650, 280);
		
		JMenuItem mntmNewMenuItem_1 = new JMenuItem("添加设备");
		mntmNewMenuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 创建对象
				DeviceAddDialog dad = new DeviceAddDialog();
				// 设置为模态属性
				dad.setModal(true);
				// 显示该窗口
				dad.setVisible(true);
				//刷新
				fillTable();
			}
		});
		mnNewMenu.add(mntmNewMenuItem_1);
		
		JMenuItem mntmNewMenuItem_13 = new JMenuItem("查询和修改");
		mnNewMenu.add(mntmNewMenuItem_13);

		JMenu mnNewMenu_1 = new JMenu("入库管理");
		menuBar.add(mnNewMenu_1);

		JMenuItem mntmNewMenuItem = new JMenuItem("新增入库");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 调转到该对象
				Device_InAddDialog diad = new Device_InAddDialog();
				// 设置为模态窗口
				diad.setModal(true);
				// 显示窗口
				diad.setVisible(true);
			}
		});
		mnNewMenu_1.add(mntmNewMenuItem);

		JMenuItem mntmNewMenuItem_7 = new JMenuItem("查询和修改");
		mntmNewMenuItem_7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Device_InFindDialog difd = new Device_InFindDialog();
				// 设置为模态窗口
				difd.setModal(true);
				// 显示
				difd.setVisible(true);
			}
		});
		mnNewMenu_1.add(mntmNewMenuItem_7);

		JMenu mnNewMenu_2 = new JMenu("出库管理");
		menuBar.add(mnNewMenu_2);

		JMenuItem mntmNewMenuItem_2 = new JMenuItem("新增出库");
		mntmNewMenuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Device_OutAddDialog doad = new Device_OutAddDialog();
				// 设置为模态属性
				doad.setModal(true);
				// 显示窗口
				doad.setVisible(true);
			}
		});
		mnNewMenu_2.add(mntmNewMenuItem_2);

		JMenuItem mntmNewMenuItem_5 = new JMenuItem("查询和修改");
		mntmNewMenuItem_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 跳转
				Device_OutFindDialog doutfind = new Device_OutFindDialog();
				// 设置为模态属性
				doutfind.setModal(true);
				// 显示此窗口
				doutfind.setVisible(true);
			}
		});
		mnNewMenu_2.add(mntmNewMenuItem_5);

		JMenu mnNewMenu_3 = new JMenu(" 还库管理");
		menuBar.add(mnNewMenu_3);

		JMenuItem mntmNewMenuItem_3 = new JMenuItem("新增还库");
		mntmNewMenuItem_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Device_ReturnAddDialog drad = new Device_ReturnAddDialog();
				// 设置为模态窗
				drad.setModal(true);
				// 显示此窗口
				drad.setVisible(true);
			}
		});
		mnNewMenu_3.add(mntmNewMenuItem_3);

		JMenuItem mntmNewMenuItem_9 = new JMenuItem("查询和修改");
		mntmNewMenuItem_9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 创建跳转的对象
				Device_ReturnFindDialog drfd = new Device_ReturnFindDialog();
				// 设置为模态框
				drfd.setModal(true);
				// 设置为可见
				drfd.setVisible(true);
			}
		});
		mnNewMenu_3.add(mntmNewMenuItem_9);

		JMenu mnNewMenu_4 = new JMenu("需求管理");
		menuBar.add(mnNewMenu_4);

		JMenuItem mntmNewMenuItem_4 = new JMenuItem("新增需求");
		mntmNewMenuItem_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Device_needAddDialog dad = new Device_needAddDialog();
				// 设置为模态属性
				dad.setModal(true);
				// 显示
				dad.setVisible(true);
			}
		});
		mnNewMenu_4.add(mntmNewMenuItem_4);

		JMenuItem mntmNewMenuItem_10 = new JMenuItem("修改和查询");
		mntmNewMenuItem_10.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 跳转
				Device_needFindDialog dfd = new Device_needFindDialog();
				// 设置为模态属性
				dfd.setModal(true);
				// 显示
				dfd.setVisible(true);
			}
		});
		mnNewMenu_4.add(mntmNewMenuItem_10);

		JMenu mnNewMenu_6 = new JMenu("个人中心");
		menuBar.add(mnNewMenu_6);

		JMenuItem mntmNewMenuItem_6 = new JMenuItem("修改密码");
		mntmNewMenuItem_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ModPassDevAdmin modpass = new ModPassDevAdmin();
				modpass.setModal(true);
				modpass.setVisible(true);
			}
		});
		mnNewMenu_6.add(mntmNewMenuItem_6);
		
		JMenuItem mntmNewMenuItem_12 = new JMenuItem("退出登录");
		mntmNewMenuItem_12.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		mnNewMenu_6.add(mntmNewMenuItem_12);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		txtInput = new JTextField();
		txtInput.setFont(new Font("宋体", Font.PLAIN, 25));
		txtInput.setBounds(258, 128, 204, 31);
		contentPane.add(txtInput);
		txtInput.setColumns(10);

		contentPane.add(scrollPane);

		JComboBox txtCB = new JComboBox();
		txtCB.setFont(new Font("宋体", Font.PLAIN, 20));
		txtCB.setModel(new DefaultComboBoxModel(new String[] { "设备编号", "设备名称" }));
		txtCB.setBounds(258, 83, 197, 30);
		contentPane.add(txtCB);

		JLabel label = new JLabel("请选择查询方式");
		label.setFont(new Font("宋体", Font.PLAIN, 25));
		label.setBounds(63, 77, 181, 38);
		contentPane.add(label);

		JButton btnNewButton = new JButton("查询");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String opString = txtCB.getSelectedItem() + "";
				// 取值
				String code = txtInput.getText();
				// 对复选框的内容进行判断
				if (opString.equals("设备编号")) {
					if (code.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "查询内容不能为空");
					} else {
						// 调用方法
						Device dev = fillTableByCo(scrollPane, code);
						if (dev == null) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "该编号不存在，请重新输入");
						}
					}
				} else if (opString.equals("设备名称")) {
					if (code.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "查询内容不能为空");
					} else {
						// 调用方法
						List<Device> list=fillTableByName(scrollPane, code);
						if (list.isEmpty()) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "该名称不存在，请重新输入");
						}
					}
				}

			}
		});
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 20));
		btnNewButton.setBounds(486, 126, 113, 31);
		contentPane.add(btnNewButton);

		JLabel lblNewLabel = new JLabel("请输入查询信息");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		lblNewLabel.setBounds(63, 125, 181, 38);
		contentPane.add(lblNewLabel);
		//
		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
//				//取值
				devCode = table.getValueAt(table.rowAtPoint(e.getPoint()), 0) + "";
				String devName = table.getValueAt(table.rowAtPoint(e.getPoint()), 1) + "";
				String nowNumString = table.getValueAt(table.rowAtPoint(e.getPoint()), 2) + "";
				String highNumString = table.getValueAt(table.rowAtPoint(e.getPoint()), 3) + "";
				String lowNumString = table.getValueAt(table.rowAtPoint(e.getPoint()), 4) + "";
				String totalNumString = table.getValueAt(table.rowAtPoint(e.getPoint()), 5) + "";
//				
//				
				// 转型
				int nowNum = Integer.parseInt(nowNumString);
				int highNum = Integer.parseInt(highNumString);
				int lowNum = Integer.parseInt(lowNumString);
				int totalNum = Integer.parseInt(totalNumString);
//				
				// 赋值
				device1 = new Device(devCode, devName, nowNum, highNum, lowNum, totalNum);

			}
		});

		table.setRowHeight(20);

		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(table, popupMenu);

		JMenuItem mntmNewMenuItem_8 = new JMenuItem(" 修改");
		mntmNewMenuItem_8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 调用带参的方法
				DeviceModDialog dmd = new DeviceModDialog(device1);
				// 设置为模态属性
				dmd.setModal(true);
				// 显示该窗口
				dmd.setVisible(true);

				fillTable();
			}
		});
		popupMenu.add(mntmNewMenuItem_8);

		JMenuItem mntmNewMenuItem_11 = new JMenuItem(" 删除");
		mntmNewMenuItem_11.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int n = JOptionPane.showConfirmDialog(null, "请问是否继续删除？删除后将无法恢复！", "删除设备编号为: " + devCode + " 的所有信息",
						JOptionPane.YES_NO_OPTION);
				if (n == 0) {
					// 创建对象
					DeviceDao dd = new DeviceDao();
					boolean flag = dd.delDev(devCode);
					if (flag) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "删除成功");
					} else {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "删除失败", "消息", JOptionPane.ERROR_MESSAGE);
					}
					// 更新
					fillTable();
				}

			}
		});
		popupMenu.add(mntmNewMenuItem_11);
		fillTable();

		JButton button = new JButton("取消");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 查询所有
				fillTable();
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 20));
		button.setBounds(613, 126, 113, 31);
		contentPane.add(button);
		
		JLabel label_1 = new JLabel("库存查询");
		label_1.setFont(new Font("宋体", Font.PLAIN, 40));
		label_1.setBounds(295, 13, 182, 47);
		contentPane.add(label_1);
		
		txtName = new JTextField();
		txtName.setFont(new Font("宋体", Font.PLAIN, 17));
		txtName.setColumns(10);
		txtName.setBounds(653, 13, 73, 25);
		contentPane.add(txtName);
		
		JLabel label_2 = new JLabel("欢迎您：");
		label_2.setFont(new Font("宋体", Font.PLAIN, 20));
		label_2.setBounds(581, 13, 80, 30);
		contentPane.add(label_2);

		// 窗口居中
		setLocationRelativeTo(null);
	}
	
	/**
	 * 带参数的构造
	 */
	public MenuFrameDevAdmin(String username) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 822, 618);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnNewMenu = new JMenu("库存管理");
		menuBar.add(mnNewMenu);
		
		//滚动条
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(63, 182, 650, 280);
		
		JMenuItem mntmNewMenuItem_1 = new JMenuItem("添加设备");
		mntmNewMenuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 创建对象
				DeviceAddDialog dad = new DeviceAddDialog();
				// 设置为模态属性
				dad.setModal(true);
				// 显示该窗口
				dad.setVisible(true);
				//刷新
				fillTable();
			}
		});
		mnNewMenu.add(mntmNewMenuItem_1);

		JMenu mnNewMenu_1 = new JMenu("入库管理");
		menuBar.add(mnNewMenu_1);

		JMenuItem mntmNewMenuItem = new JMenuItem("新增入库");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 调转到该对象
				Device_InAddDialog diad = new Device_InAddDialog();
				// 设置为模态窗口
				diad.setModal(true);
				// 显示窗口
				diad.setVisible(true);
			}
		});
		mnNewMenu_1.add(mntmNewMenuItem);

		JMenuItem mntmNewMenuItem_7 = new JMenuItem("查询和修改");
		mntmNewMenuItem_7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Device_InFindDialog difd = new Device_InFindDialog();
				// 设置为模态窗口
				difd.setModal(true);
				// 显示
				difd.setVisible(true);
			}
		});
		mnNewMenu_1.add(mntmNewMenuItem_7);

		JMenu mnNewMenu_2 = new JMenu("出库管理");
		menuBar.add(mnNewMenu_2);

		JMenuItem mntmNewMenuItem_2 = new JMenuItem("新增出库");
		mntmNewMenuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Device_OutAddDialog doad = new Device_OutAddDialog();
				// 设置为模态属性
				doad.setModal(true);
				// 显示窗口
				doad.setVisible(true);
			}
		});
		mnNewMenu_2.add(mntmNewMenuItem_2);

		JMenuItem mntmNewMenuItem_5 = new JMenuItem("查询和修改");
		mntmNewMenuItem_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 跳转
				Device_OutFindDialog doutfind = new Device_OutFindDialog();
				// 设置为模态属性
				doutfind.setModal(true);
				// 显示此窗口
				doutfind.setVisible(true);
			}
		});
		mnNewMenu_2.add(mntmNewMenuItem_5);

		JMenu mnNewMenu_3 = new JMenu("还库管理");
		menuBar.add(mnNewMenu_3);

		JMenuItem mntmNewMenuItem_3 = new JMenuItem("新增还库");
		mntmNewMenuItem_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Device_ReturnAddDialog drad = new Device_ReturnAddDialog();
				// 设置为模态窗
				drad.setModal(true);
				// 显示此窗口
				drad.setVisible(true);
			}
		});
		mnNewMenu_3.add(mntmNewMenuItem_3);

		JMenuItem mntmNewMenuItem_9 = new JMenuItem("查询和修改");
		mntmNewMenuItem_9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 创建跳转的对象
				Device_ReturnFindDialog drfd = new Device_ReturnFindDialog();
				// 设置为模态框
				drfd.setModal(true);
				// 设置为可见
				drfd.setVisible(true);
			}
		});
		mnNewMenu_3.add(mntmNewMenuItem_9);

		JMenu mnNewMenu_4 = new JMenu("需求管理");
		menuBar.add(mnNewMenu_4);

		JMenuItem mntmNewMenuItem_4 = new JMenuItem("新增需求");
		mntmNewMenuItem_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Device_needAddDialog dad = new Device_needAddDialog();
				// 设置为模态属性
				dad.setModal(true);
				// 显示
				dad.setVisible(true);
			}
		});
		mnNewMenu_4.add(mntmNewMenuItem_4);

		JMenuItem mntmNewMenuItem_10 = new JMenuItem("修改和查询");
		mntmNewMenuItem_10.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 跳转
				Device_needFindDialog dfd = new Device_needFindDialog();
				// 设置为模态属性
				dfd.setModal(true);
				// 显示
				dfd.setVisible(true);
			}
		});
		mnNewMenu_4.add(mntmNewMenuItem_10);

		JMenu mnNewMenu_6 = new JMenu("个人中心");
		menuBar.add(mnNewMenu_6);

		JMenuItem mntmNewMenuItem_6 = new JMenuItem("修改密码");
		mntmNewMenuItem_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ModPassDevAdmin modpass = new ModPassDevAdmin();
				//设置为模态属性
				modpass.setModal(true);
				//可见
				modpass.setVisible(true);
			}
		});
		mnNewMenu_6.add(mntmNewMenuItem_6);
		
		JMenuItem mntmNewMenuItem_12 = new JMenuItem("退出登录");
		mntmNewMenuItem_12.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//跳转
				UserLoginFrame user = new UserLoginFrame();
				setVisible(false);
				user.setVisible(true);
			}
		});
		mnNewMenu_6.add(mntmNewMenuItem_12);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		txtInput = new JTextField();
		txtInput.setFont(new Font("宋体", Font.PLAIN, 25));
		txtInput.setBounds(258, 128, 204, 31);
		contentPane.add(txtInput);
		txtInput.setColumns(10);

		contentPane.add(scrollPane);

		JComboBox txtCB = new JComboBox();
		txtCB.setFont(new Font("宋体", Font.PLAIN, 20));
		txtCB.setModel(new DefaultComboBoxModel(new String[] { "设备编号", "设备名称" }));
		txtCB.setBounds(258, 83, 197, 30);
		contentPane.add(txtCB);

		JLabel label = new JLabel("请选择查询方式");
		label.setFont(new Font("宋体", Font.PLAIN, 25));
		label.setBounds(63, 77, 181, 38);
		contentPane.add(label);

		JButton btnNewButton = new JButton("查询");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String opString = txtCB.getSelectedItem() + "";
				// 取值
				String code = txtInput.getText();
				// 对复选框的内容进行判断
				if (opString.equals("设备编号")) {
					if (code.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "查询内容不能为空");
					} else {
						// 调用方法
						Device dev = fillTableByCo(scrollPane, code);
						if (dev == null) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "该编号不存在，请重新输入");
						}
					}
				} else if (opString.equals("设备名称")) {
					if (code.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "查询内容不能为空");
					} else {
						// 调用方法
						List<Device> list=fillTableByName(scrollPane, code);
						if (list.isEmpty()) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "该名称不存在，请重新输入");
						}
					}
				}

			}
		});
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 20));
		btnNewButton.setBounds(486, 126, 113, 31);
		contentPane.add(btnNewButton);

		JLabel lblNewLabel = new JLabel("请输入查询信息");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		lblNewLabel.setBounds(63, 125, 181, 38);
		contentPane.add(lblNewLabel);
		//
		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
//				//取值
				devCode = table.getValueAt(table.rowAtPoint(e.getPoint()), 0) + "";
				String devName = table.getValueAt(table.rowAtPoint(e.getPoint()), 1) + "";
				String nowNumString = table.getValueAt(table.rowAtPoint(e.getPoint()), 2) + "";
				String highNumString = table.getValueAt(table.rowAtPoint(e.getPoint()), 3) + "";
				String lowNumString = table.getValueAt(table.rowAtPoint(e.getPoint()), 4) + "";
				String totalNumString = table.getValueAt(table.rowAtPoint(e.getPoint()), 5) + "";
//				
//				
				// 转型
				int nowNum = Integer.parseInt(nowNumString);
				int highNum = Integer.parseInt(highNumString);
				int lowNum = Integer.parseInt(lowNumString);
				int totalNum = Integer.parseInt(totalNumString);
//				
				// 赋值
				device1 = new Device(devCode, devName, nowNum, highNum, lowNum, totalNum);

			}
		});

		table.setRowHeight(20);

		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(table, popupMenu);

		JMenuItem mntmNewMenuItem_8 = new JMenuItem(" 修改");
		mntmNewMenuItem_8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 调用带参的方法
				DeviceModDialog dmd = new DeviceModDialog(device1);
				// 设置为模态属性
				dmd.setModal(true);
				// 显示该窗口
				dmd.setVisible(true);

				fillTable();
			}
		});
		popupMenu.add(mntmNewMenuItem_8);

		JMenuItem mntmNewMenuItem_11 = new JMenuItem(" 删除");
		mntmNewMenuItem_11.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int n = JOptionPane.showConfirmDialog(null, "请问是否继续删除？删除后将无法恢复！", "删除设备编号为: " + devCode + " 的所有信息",
						JOptionPane.YES_NO_OPTION);
				if (n == 0) {
					// 创建对象
					DeviceDao dd = new DeviceDao();
					boolean flag = dd.delDev(devCode);
					if (flag) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "删除成功");
					} else {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "删除失败", "消息", JOptionPane.ERROR_MESSAGE);
					}
					// 更新
					fillTable();
				}

			}
		});
		popupMenu.add(mntmNewMenuItem_11);
		fillTable();
		scrollPane.setViewportView(table);

		JButton button = new JButton("取消");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 查询所有
				txtInput.setText("");
				fillTable();
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 20));
		button.setBounds(613, 126, 113, 31);
		contentPane.add(button);
		
		JLabel label_1 = new JLabel("库存查询");
		label_1.setFont(new Font("宋体", Font.PLAIN, 40));
		label_1.setBounds(295, 13, 182, 47);
		contentPane.add(label_1);
		
		txtName = new JTextField();
		//设置为不可编辑
		txtName.setEditable(false);
		//创建对象
		Device_AdminDao dad = new Device_AdminDao();
		//调用查找的方法
		Device_Admin da=dad.findDevAdminById(username);
		txtName.setText(da.getUsercode());
		txtName.setFont(new Font("宋体", Font.PLAIN, 17));
		txtName.setColumns(10);
		txtName.setBounds(653, 13, 73, 25);
		contentPane.add(txtName);
		
		JLabel label_2 = new JLabel("欢迎您：");
		label_2.setFont(new Font("宋体", Font.PLAIN, 20));
		label_2.setBounds(581, 13, 80, 30);
		contentPane.add(label_2);



		// 窗口居中
		setLocationRelativeTo(null);
		
	}

	/**
	 * 查询所有，并输出在表格里
	 * 
	 * @param scrollPane
	 */
	public void fillTable() {
		// 创建对象
		DeviceDao dd = new DeviceDao();
		// 调用查找所有的方法
		List<Device> list = dd.findDeviceAll();
		// 获取list长度
		int row = list.size();
		// 创建二维数组
		Object[][] obj = new Object[row][6];
		// 迭代遍历
		for (int i = 0; i < obj.length; i++) {
			// 取值
			Device device = list.get(i);
			// 赋值
			obj[i][0] = device.getDevice_code();
			obj[i][1] = device.getDevice_name();
			obj[i][2] = device.getNow_num();
			obj[i][3] = device.getHigh_num();
			obj[i][4] = device.getLow_num();
			obj[i][5] = device.getTotal_num();
		}

		table.setModel(new DefaultTableModel(obj,
				new String[] { "\u8BBE\u5907\u7F16\u53F7", "\u8BBE\u5907\u540D\u79F0", "\u73B0\u6709\u5E93\u5B58",
						"\u6700\u5927\u5E93\u5B58", "\u6700\u5C0F\u5E93\u5B58", "\u5E93\u5B58\u603B\u6570" }));
		

	}

	/**
	 * 通过编号查询，并输出在表格里
	 * 
	 * @param scrollPane
	 */
	private Device fillTableByCo(JScrollPane scrollPane, String code) {
		// 创建对象
		DeviceDao dd = new DeviceDao();
		// 调用查找de方法
		Device device = dd.findDevByCo(code);
		//
		if (device != null) {
			// 创建二维数组
			Object[][] obj = new Object[1][6];
			// 赋值
			obj[0][0] = device.getDevice_code();
			obj[0][1] = device.getDevice_name();
			obj[0][2] = device.getNow_num();
			obj[0][3] = device.getHigh_num();
			obj[0][4] = device.getLow_num();
			obj[0][5] = device.getTotal_num();

			table.setModel(new DefaultTableModel(obj,
					new String[] { "\u8BBE\u5907\u7F16\u53F7", "\u8BBE\u5907\u540D\u79F0", "\u73B0\u6709\u5E93\u5B58",
							"\u6700\u5927\u5E93\u5B58", "\u6700\u5C0F\u5E93\u5B58", "\u5E93\u5B58\u603B\u6570" }));
			scrollPane.setViewportView(table);
		}
		return device;
	}

	/**
	 * 根据名称查询
	 * 
	 * @param scrollPane
	 */
	private List<Device> fillTableByName(JScrollPane scrollPane, String code) {
		// 创建对象
		DeviceDao dd = new DeviceDao();
		// 调用查找所有的方法
		List<Device> list = dd.findDeviceByName(code);
		if (list != null) {
			// 获取list长度
			int row = list.size();
			// 创建二维数组
			Object[][] obj = new Object[row][6];
			// 迭代遍历
			for (int i = 0; i < obj.length; i++) {
				// 取值
				Device device = list.get(i);
				// 赋值
				obj[i][0] = device.getDevice_code();
				obj[i][1] = device.getDevice_name();
				obj[i][2] = device.getNow_num();
				obj[i][3] = device.getHigh_num();
				obj[i][4] = device.getLow_num();
				obj[i][5] = device.getTotal_num();
			}

			table.setModel(new DefaultTableModel(obj,
					new String[] { "\u8BBE\u5907\u7F16\u53F7", "\u8BBE\u5907\u540D\u79F0", "\u73B0\u6709\u5E93\u5B58",
							"\u6700\u5927\u5E93\u5B58", "\u6700\u5C0F\u5E93\u5B58", "\u5E93\u5B58\u603B\u6570" }));
			scrollPane.setViewportView(table);
		}
		return list;
	}

	/**
	 * 右键
	 * 
	 * @param component
	 * @param popup
	 */
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
